package com.zc.im.service.customer.impl;

import com.zc.im.mapper.customer.ZcCustomerServiceRelationMapper;
import com.zc.im.model.customer.ZcCustomerServiceRelation;
import com.zc.im.model.customer.ZcCustomerServiceRelationExample;
import com.zc.im.model.customer.ZcCustomerServiceRelationKey;
import com.zc.im.service.customer.CustomerServiceRelationService;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by wangcc on 2017/7/9.
 */
@Service
public class CustomerServiceRelationServiceImpl implements CustomerServiceRelationService {


    @Autowired
    private ZcCustomerServiceRelationMapper zcCustomerServiceRelationMapper;
    private static Logger logger = Logger.getLogger(CustomerServiceRelationServiceImpl.class);

    /**
     * 客服客户对应关系添加
     * @param zcCustomerServiceRelationModel
     * @return
     */
    @Override
    public Integer insertCustomerServiceRelation(ZcCustomerServiceRelation zcCustomerServiceRelationModel) {
        Integer CustomerServiceRelationId = null;
        if (zcCustomerServiceRelationModel == null) {
            logger.info("insert CustomerServiceRelation is null");
            return CustomerServiceRelationId;
        }
        zcCustomerServiceRelationModel.setStatus(ImGolbalTypeContext.STATUS_VALID);
        zcCustomerServiceRelationModel.setCreatedTime(new Date());
        try {
            CustomerServiceRelationId = zcCustomerServiceRelationMapper.insertSelective(zcCustomerServiceRelationModel);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return CustomerServiceRelationId;
    }

    /**
     * 删除客服客户对应关系
     * @param id
     * @return
     */
    @Override
    public Integer deleteCustomerServiceRelation(Integer id) {
        Integer CustomerServiceRelationId = null;
        if (id == null) {
            logger.info("delete customerservicerelation is null");
            return CustomerServiceRelationId;
        }
        try {
            ZcCustomerServiceRelationKey key = new ZcCustomerServiceRelationKey();
            key.setId(id);
            CustomerServiceRelationId = zcCustomerServiceRelationMapper.deleteByPrimaryKey(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return CustomerServiceRelationId;
    }

    /**
     * 更新客服客户对应关系
     * @param zcCustomerServiceRelationModel
     * @return
     */
    @Override
    public Integer updateCustomerServiceRelation(ZcCustomerServiceRelation zcCustomerServiceRelationModel) {
        Integer CustomerServiceRelationId = null;
        if (zcCustomerServiceRelationModel == null) {
            logger.info("update customerservicerelation is null");
            return CustomerServiceRelationId;
        }
        zcCustomerServiceRelationModel.setUpdatedTime(new Date());
        try {
            ZcCustomerServiceRelationExample example = new ZcCustomerServiceRelationExample();
            ZcCustomerServiceRelationExample.Criteria criteria =  example.createCriteria();
            CustomerServiceRelationId = zcCustomerServiceRelationMapper.updateByExample(zcCustomerServiceRelationModel, example);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return CustomerServiceRelationId;
    }


    /**
     * 查询客服对应的客服
     * @param zcCustomerServiceRelationModel
     * @return
     */
    @Override
    public ZcCustomerServiceRelation findCustomerServiceRelation(ZcCustomerServiceRelation zcCustomerServiceRelationModel) {
        ZcCustomerServiceRelation serviceRelation = null;
        if (zcCustomerServiceRelationModel == null) {
            logger.info("query customerservicerelation is null");
            return serviceRelation;
        }
        try {
            ZcCustomerServiceRelationExample example = new ZcCustomerServiceRelationExample();
            ZcCustomerServiceRelationExample.Criteria criteria =  example.createCriteria();
            criteria.andCustomerServiceTypeEqualTo(zcCustomerServiceRelationModel.getCustomerServiceType());

            List<ZcCustomerServiceRelation> serviceRelations  = zcCustomerServiceRelationMapper.selectByExample(example);
            if(serviceRelations == null || serviceRelations.size() == 0){
                serviceRelation = serviceRelations.get(0);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return serviceRelation;
    }
}
